var nowpage = 1
//全体发送
document.getElementById('message_sendall').onclick = function(){
    var form_messageall = {
        content:message_main_contentall.value,
        title:message_main_titleall.value
    }
    $.ajax({
        url:initObj.url + "/api/role/sendMessageToAllPerson",
        data:JSON.stringify(form_messageall),
        // data:form_messageall,
        datatype:'json',
        type:'post',
        contentType: "application/json;charset=utf-8",
        beforeSend: function(request) {
           request.setRequestHeader("token", form.token);
        //    console.log(form.token)
        },
        success:function(res){
            // console.log(res)
            alert(res.msg)
        }
    })
}

//获取商户号
$.ajax({
    url:initObj.url + "/api/user/getAllUserMerchantNo",
    datatype:'json',
    type:'get',
    // contentType: "application/json;charset=utf-8",
    beforeSend: function(request) {
       request.setRequestHeader("token", form.token);
    //    console.log(form.token)
    },
    success:function(res){
        console.log(res)
        message_merchantNo.innerHTML = ""
        for(var i=0;i<res.data.length;i++){
            message_merchantNo.innerHTML += "<option value='"+res.data[i]+"'>"+res.data[i]+"</option>"
        }
        $(function(){
	        $('select').searchableSelect();
        });
    }
})
//发一条消息
document.getElementById('message_send').onclick = function(){
    var form_message = {
        content:message_main_content.value,
        title:message_main_title.value
    }
    $.ajax({
        url:initObj.url + "/api/role/sendOneMessageToPerson/" + document.getElementById('message_merchantNo').value,
        data:JSON.stringify(form_message),
        // data:form_messageall,
        datatype:'json',
        type:'post',
        contentType: "application/json;charset=utf-8",
        beforeSend: function(request) {
           request.setRequestHeader("token", form.token);
        //    console.log(form.token)
        },
        success:function(res){
            // console.log(res)
            alert(res.msg)
        }
    })
}
xiaoxifabu.onclick = function(){
    message_liebiao.style.display = "none"
    message_fabu.style.display = "block"
    xiaoxifabu.style.fontWeight = "bold"
    xiaoxiliebiao.style.fontWeight = "normal"
}
xiaoxiliebiao.onclick = function(){
    message_fabu.style.display = "none"
    message_liebiao.style.display = "block"
    xiaoxiliebiao.style.fontWeight = "bold"
    xiaoxifabu.style.fontWeight = "normal"
    getMessagedata(form1)
}

//消息列表

var form1 = {
    size: 10,
    page: 1,
}
var form2 = {
    total: 2000,
    pageSize: 10,
    showTotal: true,
    showTo: true,
    change: function ( i ) {
        createContent( i );
    }
}
var $content = $( ".content" );
function createContent (i) {
    form1.page = i
    getMessagedata(form1)
}
function changeinfo(id,value){
    form1[id] = value
    // console.log(form1)
}

Helper.ui.page("#page-3", form2);

function getMessagedata(form1){
    $.ajax({
        url:initObj.url + "/api/role/adminFindAllMessage/"+form1.page+"/"+form1.size,
        // data:JSON.stringify(form1),
        // data:form1,
        datatype:'json',
        type:'get',
        beforeSend: function(request) {
           request.setRequestHeader("token", form.token);
        //    console.log(form.token)
        },
        success:function(res){
            console.log(res)
            document.getElementById('selectAll').innerHTML = ""
            for(var i=0;i<res.data.content.length;i++){
                // var isRead = (res.data.content[i].isRead=='未读'?1:0)
                document.getElementById('selectAll').innerHTML += 
                "<li><span style='width:10vw;display:inline-block;overflow: hidden;height:2.8vh'>"+res.data.content[i].title+
                "</span><span style='width:15vw;display:inline-block;overflow: hidden;height:2.8vh'>"+res.data.content[i].content+
                "</span><span style='width:15vw;display:inline-block'>"+res.data.content[i].createTime+
                "</span><button style='color:#f6f6f6;background-color:#666;border-radius:15px;display:inline-block;margin-left:5vw' onclick='showDetail("+
                res.data.content[i].id+")'>查看内容</button></li>"
            }
            if(form2.total !== res.data.totalElements){
                form2.total = res.data.totalElements
                Helper.ui.page("#page-3", form2);
                nowpage++
            }
        }
    })
}
function showDetail(id){
    $.ajax({
        url:initObj.url + "/api/role/showMessage/"+id,
        // data:JSON.stringify(form1),
        // data:form1,
        datatype:'json',
        type:'get',
        beforeSend: function(request) {
           request.setRequestHeader("token", form.token);
        //    console.log(form.token)
        },
        success:function(res){
            // console.log(res)
            $('#message_content').html(res.data.content)
            $('#message_createTime').html(res.data.createTime)
            $('#message_title').html(res.data.title)
        }
    })
    document.getElementById('message_detail').style.display = "block"
}
function message_close(){
    getMessagedata(form1)
    document.getElementById('message_detail').style.display = "none"
}
getMessagedata(form1)

